<?php
session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");

$user = $_SESSION['user'];
$order = mysql_real_escape_string($_GET['id']);

$result = mysql_query("SELECT * FROM user WHERE id = $user");
$row = mysql_fetch_array($result);
$username = $row['username'];
$access_level = $row['type'];

$result = mysql_query("SELECT * FROM referral_order WHERE id = $order");
$row = mysql_fetch_array($result);
$customer = $row['user'];

$order_result = mysql_query("SELECT * FROM referral_order WHERE id = $order");

if(mysql_num_rows($order_result) == 0)
	header("Location: home.php");

$order_row = mysql_fetch_array($order_result);

$user_result = mysql_query("SELECT * FROM user WHERE id = ".$order_row['user']);
$user_row = mysql_fetch_array($user_result);

if($access_level != 2 && $user != $order_row['user'])
	header("Location: home.php");

?>
<!doctype html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, maximum-scale=1, initial-scale=1, user-scalable=0">
    <link rel="icon" href="http://www.veteranboosters.com/wp-content/uploads/2014/04/favicon4.png" type="image/x-icon"/>
    <link rel="shortcut icon" href="http://www.veteranboosters.com/wp-content/uploads/2014/04/favicon4.png" type="image/x-icon"/>
  <!-- Always force latest IE rendering engine or request Chrome Frame -->
  <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">

  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,800">

  <!-- Use title if it's in the page YAML frontmatter -->
  <title>Veteran Boosters Order Tracking</title>


  <!--[if lt IE 9]>
  <script src="../../javascripts/vendor/html5shiv.js" type="text/javascript"></script>
  <script src="../../javascripts/vendor/excanvas.js" type="text/javascript"></script>
  <![endif]-->
<link rel="stylesheet" href="css/bootstrap.min.css" />
<link rel="stylesheet" href="css/font-awesome.css" />
<link rel="stylesheet" href="css/fullcalendar.css" />
<link rel="stylesheet" href="css/jquery.jscrollpane.css" />
<link rel="stylesheet" href="css/unicorn.css" />
 <link href="stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
  <script src="javascripts/application.js" type="text/javascript"></script>
  <script src="javascripts/bootpag.js"></script>
</head>
<?php
	include("top.php");
?>
     <style>
         .input-append{
             float: left;
             width: 100%;
             position: relative;
             height: auto;
             margin-top: -35px;
         }
         .input-append input[type="text"]#msg-box{
             float: left;
             width: 100%;
             position: relative;
             padding: 5px;
             margin-bottom: 10px;

         }
         .input-append .add-on{
             float: left;
             width: 100%;
             position: relative!important;
             margin-top: 0px!important;
         }
     </style>
  <div class="container-fluid padded">
    <div class="row-fluid">

      <!-- Breadcrumb line -->

      <div id="breadcrumb">
	   <a href="/" title="" class="tip-bottom" data-original-title="Go to Home"><i class="fa fa-home"></i> Home</a>
		<a href="#">Members</a>
		<a href="#" class="current">Overview    </a>
       </div>
    </div>
  </div>

  <div class="container-fluid padded">
  <div class = "row-fluid">
  <div class="col-lg-12">
      <div class="box">
         <div class="box-header">
            <span class="title">Order Details: Order ID <?php echo $order; ?></span>
         </div>
         <div class="box-content">
            <table class="table table-bordered table-striped table-hover">
               <thead>
                  <tr>
					 <td>Summoner</td>
					 <td>Number of Referrals</td>
					 <td>Referral Link</td>
                  </tr>
               </thead>
               <tbody>
                  <tr>
				  <td><?php echo $user_row['summoner']; ?></td>
				  <td><?php echo $order_row['referrals']; ?></td>
				  <td><?php echo $order_row['link']; ?></td>
				  </tr>
				  </tbody>
            </table>
         </div>
      </div>
   </div>
   </div>
   <div class="modal fade" id="gift-modal">
   <div class="modal-dialog">
   <div class="modal-content">
               <form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="gift-form" class="form-horizontal"> 
                  <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                     <h4>Send Gift For Booster</h4>
                  </div>
                  <div class="modal-body">
                     <div class="control-group" style="margin-bottom: 0px;">
                        <label class="control-label" for="gift-value">Gift Value:</label>
                        <div class="controls">
                           <select class="input-big" id="gift-value" name="gift-value">
                              <option value="5" label="$5">$5</option>
                              <option value="10" label="$10">$10</option>
                              <option value="15" label="$15">$15</option>
                              <option value="20" label="$20">$20</option>
                              <option value="25" label="$25">$25</option>
                              <option value="30" label="$30">$30</option>
                              <option value="40" label="$40">$40</option>
                              <option value="50" label="$50">$50</option>
                           </select>
                        </div>
                     </div>
                     <input type="hidden" name="cmd" value="_xclick">
                     <input type="hidden" name="business" value="Admin@VeteranBoosters.com">
                     <input type="hidden" name="item_name" id="name" value="Gift for booster">
                     <input type="hidden" name="item_number" value="3">
                     <input type="hidden" name="amount" id="cost" value="5">
                     <input type="hidden" name="currency_code" value="USD">
                     <input type="hidden" value="order=<?php echo $order; ?>&amp;booster=4" name="custom" id="custom">
                     <input type="hidden" name="notify_url" value="http://veteranboosters.com/confirm_gift.php">
                     <input type="hidden" name="return" value="http://veteranboosters.com/orders/referral_order.php?id=<?php echo $order; ?>">
                  </div>
                  <div class="modal-footer">
                     <a href="#" class="btn btn-default" data-dismiss="modal">Close</a>
                     <button type="submit" class="btn btn-success btn-xs" id="save">Continue to PayPal</button>
                  </div>
               </form>
            </div>
   </div>
   </div>
            <div id="gifts" style = "padding: 7px 11px">
                  <button type="button" class="btn btn-large btn-block btn-green" data-toggle="modal" data-target="#gift-modal"><i class="icon-gift icon-white"></i> Send Gift For Booster!</button>
            </div>
   <?php
		if($access_level == 2)
		{
			

			//echo '<div class = "row-fluid"><div class = "span12">';
			//echo '<a href = "flag.php?id='.$order_row['id'].'" class="btn btn-large btn-block btn-red">Contact Admin (Creates Ticket)</a><br />';

			if($order_row['complete'] != 1)
			{
				echo '<table class = "table table-normal"><tr><td width = "50%"><a onclick = "check_confirm(event)" href = "flag.php?id='.$order_row['id'].'" class="btn btn-large btn-block btn-red">Contact Admin (Creates Ticket)</a></td><td><a class="btn btn-large btn-block btn-green" onclick = "check_confirm(event)" href = "complete_referral.php?id='.$order.'">Mark As Complete (Pending Admin Verification)</a></td></tr></table>';

				echo '</div>';
			}
			else
			{
				echo '</div>';
			}
			
		}
		else if($type == 0)
		{
			
			//echo '<div class = "row-fluid"><div class = "span12">';
			//echo '<a href = "flag.php?id='.$order_row['id'].'" class="btn btn-large btn-block btn-red">Contact Admin (Creates Ticket)</a><br />';
			echo '<table class = "table table-normal"><tr><td width = "50%"><a onclick = "check_confirm(event)" href = "flag.php?id='.$order_row['id'].'" class="btn btn-large btn-block btn-red">Contact Admin (Creates Ticket)</a></td><td><a class="btn btn-large btn-block btn-green" onclick = "check_confirm(event)" href = "complete_referral.php?id='.$order.'">Mark as complete (pending admin verification)</a></td></tr></table>';
			echo '</div>';
		}
	?>
<div class="row-fluid">
   <div class="col-lg-12">
      <div class="box support">
         <div class="box-header">
            <div class="title">Customer Chat</div>
         </div>

         <div class="box-content">
            
            <ul class="chat-box" id="refresh" style = "height: 489px;">
               <div id="chat-messages-inner">
			   <?php
					$result = mysql_query("SELECT * FROM referral_chat WHERE referral_order = $order ORDER BY timestamp DESC");

					$messages = array();

					while($row = mysql_fetch_array($result))
					{
						$subresult = mysql_query("SELECT * FROM user WHERE id = ".$row['user']);
						$subrow = mysql_fetch_array($subresult);

						$row['name'] = $subrow['username'];
						$row['email'] = $subrow['paypal'];
						$row['type'] = $subrow['type'];

						array_push($messages, $row);
					}

					$x = 0;

					while(isset($messages[$x]))
					{
						if($messages[$x]['type'] == 0)
						{
							echo '<li class="arrow-box-right gray" id="msg-'.$messages[$x]['id'].'">';
							echo '<div class="avatar"><img class="avatar-small" src="images/avatars/'.$messages[$x]['type'].'.png"></div>';
							echo '<div class="info"><span class="name"><strong>'.$messages[$x]['name'].'</strong>';
                            echo '<span class="badge badge-gray">customer</span>';

							if($type == 2)
								echo "<span class='badge badge-red' style = 'margin-left: 5px; cursor: pointer;' onclick = 'delete_order_chat(".$messages[$x]['id'].")'>Remove</span>";

							echo '</span>';
							echo '<span class="time">'.date("d M Y g:i:sA", $messages[$x]['timestamp']).'</span>';
							echo '</div>'.$messages[$x]['message'].'</li>';
							echo '<li class="divider"></li>';
						}
						else if($messages[$x]['type'] == 1)
						{
							echo '<li class="arrow-box-left gray" id="msg-'.$messages[$x]['id'].'">';
							echo '<div class="avatar"><img class="avatar-small" src="images/avatars/'.$messages[$x]['type'].'.png"></div>';
							echo '<div class="info"><span class="name"><strong>'.$messages[$x]['name'].'</strong>';
                            echo '<span class="badge badge-blue">booster</span>';

							if($type == 2)
								echo "<span class='badge badge-red' style = 'margin-left: 5px; cursor: pointer;' onclick = 'delete_order_chat(".$messages[$x]['id'].")'>Remove</span>";

							echo '</span>';
							echo '<span class="time">'.date("d M Y g:i:sA", $messages[$x]['timestamp']).'</span>';
							echo '</div>'.$messages[$x]['message'].'</li>';
							echo '<li class="divider"></li>';
						}
						else if($messages[$x]['type'] == 2)
						{
							echo '<li class="arrow-box-left gray" id="msg-'.$messages[$x]['id'].'">';
							echo '<div class="avatar"><img class="avatar-small" src="images/avatars/'.$messages[$x]['type'].'.png"></div>';
							echo '<div class="info"><span class="name"><strong>'.$messages[$x]['name'].'</strong>';
                            echo '<span class="badge badge-red">administrator</span>';

							if($type == 2)
								echo "<span class='badge badge-red' style = 'margin-left: 5px; cursor: pointer;' onclick = 'delete_order_chat(".$messages[$x]['id'].")'>Remove</span>";

							echo '</span>';
							echo '<span class="time">'.date("d M Y g:i:sA", $messages[$x]['timestamp']).'</span>';
							echo '</div>'.$messages[$x]['message'].'</li>';
							echo '<li class="divider"></li>';
						}

						$x++;
					}
				?>
         
</div>
            </ul>
         </div>

         <div class="box-footer flat padded" style = "height: 56px">
		 <?php
				echo '
            <div class="input-append" style="width: 100%; float:left; position: relative; height:auto">
               <input type="text" name="msg-box" id="msg-box"  style="height: 100%; padding:5px;margin-bottom:10px;
width: 100%; float:left; position: relative; " onkeyup = "check_chat_send(event)">
			   <input type="hidden" id="order" value="'.$order.'" />
               <ul class="add-on" style = "width: 100%; float:left; position: relative;">';
			   if($type == 2 || $type == 3)
				{
					$canned_result = mysql_query("SELECT * FROM canned_message WHERE level = 2 ORDER BY defined_order");

					$select_buffer = '<select id = "admin_message"><option value = "0">Custom (enter above)</option>';

					while($canned_row = mysql_fetch_array($canned_result))
					{
						$select_buffer .= "<option value = '".$canned_row['id']."'>".$canned_row['name']."</options>";
					}

					$select_buffer .= "</select>";

				//	echo '<li>'.$select_buffer.'</li>';
				}
				if($type == 1 || $type == 2 || $type == 3)
				{
					$canned_result = mysql_query("SELECT * FROM canned_message WHERE level = 1 ORDER BY defined_order");

					$select_buffer = '<select id = "booster_message"><option value = "0">Custom (enter above)</option>';

					while($canned_row = mysql_fetch_array($canned_result))
					{
						$select_buffer .= "<option value = '".$canned_row['id']."'>".$canned_row['name']."</options>";
					}

					$select_buffer .= "</select>";

				//	echo '<li>'.$select_buffer.'</li>';
				}
				
			   echo '
                  <li>
                     <button class="btn btn-success btn-xs" type="submit" onclick = "chat_send()">Send</button>
                  </li>
               </ul>
            </div>';
			
			?>
         </div>
      </div>
   </div>
   
   <?php
	//	include("facebook.php");
   ?>
<?php
	if($type == 2 || $type == 3)
	{
?>
        <!--
<div class="row-fluid">
   <div class="col=lg-12">
      <div class="box">
         <div class="box-header">
            <span class="title">Email Notifications</span>
         </div>
         <div class="box-content">
            <table class="table table-bordered table-striped table-hover">
               <thead>
                  <tr>
					 <td>Name</td>
					 <td>Action</td>
                  </tr>
               </thead>
               <tbody>
			   <form method = 'post' action = 'send_email_notification.php'>
			   <input type = "hidden" value = "<?php echo $order; ?>" name = "order" />
			   <tr><td><select name = "message">
			   <?php
					$result = mysql_query("SELECT * FROM email_notification ORDER BY defined_order");

					while($row = mysql_fetch_array($result))
					{
						echo "<option value = '".$row['id']."'>".$row['name']."</option>";
					}
			   ?>
			   </select></td>
			   <td><input type = 'submit' class='btn btn-success btn-xs' value = 'Send' /></td>
			   </form>
			   </tbody>
            </table>
         </div>
      </div>
   </div>
</div>    -->
<?php
	}
?>
</div>
<script>
    $(document).ready(function(){
        $('.modal-backdrop .fade').hide();
    });
function check_chat_send (event)
{
	if(event.keyCode == 13)
	{
		if(document.getElementById("msg-box").value != "")
		{
			chat_send();
		}
	}
}
function chat_send ()
{
	if(document.getElementById("msg-box").value != "")
	{
		var ajaxHandler = new XMLHttpRequest();

		ajaxHandler.onreadystatechange = function ()
		{
			if(ajaxHandler.readyState == 4)
			{
				update_order_chat();
				document.getElementById("msg-box").value = "";
			}
		}

		var form = new FormData();
		form.append("msg-box", document.getElementById("msg-box").value);
		form.append("order", document.getElementById("order").value);

		ajaxHandler.open("POST", "referral-chat-reply.php");
		ajaxHandler.send(form);
	}
}
function delete_order_chat (id)
{
	var ajaxHandler = new XMLHttpRequest();

	var form = new FormData();
	form.append("id", id);

	ajaxHandler.open("POST", "delete_referral_chat.php");
	ajaxHandler.send(form);

	var target = document.getElementById("msg-"+id);
	target.parentNode.removeChild(target);
}
function update_order_chat ()
{
	var ajaxHandler = new XMLHttpRequest();

	ajaxHandler.onreadystatechange = function ()
	{
		if(ajaxHandler.readyState == 4)
		{
			var chat_box = document.getElementById("chat-messages-inner");

			var messages = eval( '(' + ajaxHandler.responseText + ')' );

			var i = 0;

			chat_box.innerHTML = "";
			var html_buffer = "";

			while(messages[i] != null)
			{
				if(messages[i]['type'] != 0)
				{
					html_buffer += '<li class="arrow-box-left gray" id="msg-'+messages[i]['id']+'">';
					html_buffer += '<div class="avatar"><img class="avatar-small" src="images/avatars/'+messages[i]['type']+'.png"></div>';
					html_buffer += '<div class="info"><span class="name"><strong>'+messages[i]['name']+'</strong>';

					if(messages[i]['type'] == 1)
						html_buffer += '<span class="badge badge-blue">booster</span>';
					else
						html_buffer += '<span class="badge badge-red">administrator</span>';

					<?php
						if($type == 2)
							echo "html_buffer += '<span class=\"badge badge-red\" style = \"margin-left: 5px; cursor: pointer;\" onclick = \"delete_order_chat('+messages[i]['id']+')\">Remove</span>';";
					?>

					html_buffer += '</span><span class="time">'+messages[i]['time']+'</span></div>';
					html_buffer += messages[i]['message'];
					html_buffer += '</li><li class="divider"></li>';
				}
				else
				{
					html_buffer += '<li class="arrow-box-right gray" id="msg-'+messages[i]['id']+'">';
					html_buffer += '<div class="avatar"><img class="avatar-small" src="images/avatars/'+messages[i]['type']+'.png"></div>';
					html_buffer += '<div class="info"><span class="name"><strong>'+messages[i]['name']+'</strong>';
					html_buffer += '<span class="badge badge-gray">customer</span>';
					<?php
						if($type == 2)
							echo "html_buffer += '<span class=\"badge badge-red\" style = \"margin-left: 5px; cursor: pointer;\" onclick = \"delete_order_chat('+messages[i]['id']+')\">Remove</span>';";
					?>
					html_buffer += '</span><span class="time">'+messages[i]['time']+'</span></div>';
					html_buffer += messages[i]['message'];
					html_buffer += '</li><li class="divider"></li>';
				}
				i++;
			}

			chat_box.innerHTML = html_buffer;
		}
	}
	
	var form = new FormData();
	form.append("order", <?php echo $order; ?>);

	ajaxHandler.open("POST", "referral-chat-refresh.php");
	ajaxHandler.send(form);
}
function updateTimer ()
{
	update_order_chat();
	setTimeout(updateTimer, 10000);
}
window.onload = function ()
{
	updateTimer();
}
function check_specified (event)
{
	if(document.getElementById("booster-name").value == 0)
	{
		alert("Please select a booster.");
		event.preventDefault();
		return false;
	}
}
function check_confirm (event)
{
	if(!confirm("Are you sure?"))
	{
		event.preventDefault();
		return false;
	}
}

</script>
</div>

<?php
	include("bottom.php");
?>